﻿using MediatR;
using RBAC_Uthorization.API.Application.Command.RoleMenuFunCommand;
using RBAC_Uthorization.API.DTO;
using RBAC_Uthorization.Domain;
using RBAC_Uthorization.Domain.Entity;
using RBAC_Uthorization.Infrstructre.Impl;
using RBAC_Uthorization.Infrstructre.Interface;
using StackExchange.Redis;

namespace RBAC_Uthorization.API.Application.CommandHandlers.RoleMenuFunHandler
{
    public class RoleMenuFunUpdateCommandHandlers : IRequestHandler<RoleMenuFunUpdateCommand, ApiResult<int>>
    {
        private readonly IRoleMenuFuncRepository _roleMenuFunRepository;
        private readonly IRoleRepository _roleRepository;
        private readonly IMenuRepository _menuRepository;
        private readonly IFunctionRepository _functionRepository;
        private readonly IRoleMenuRepository _roleMenuRepository;
        private readonly ILogger<RoleMenuFunUpdateCommandHandlers> _logger;

        public RoleMenuFunUpdateCommandHandlers(IRoleMenuFuncRepository roleMenuFunRepository, IRoleRepository roleRepository, IMenuRepository menuRepository, IFunctionRepository functionRepository, IRoleMenuRepository roleMenuRepository, ILogger<RoleMenuFunUpdateCommandHandlers> logger)
        {
            _roleMenuFunRepository = roleMenuFunRepository;
            _roleRepository = roleRepository;
            _menuRepository = menuRepository;
            _functionRepository = functionRepository;
            _roleMenuRepository = roleMenuRepository;
            _logger = logger;
            
        }

        public async Task<ApiResult<int>> Handle(RoleMenuFunUpdateCommand request, CancellationToken cancellationToken)
        {
            try
            {
               

                //先删除原有的菜单功能
                var roleMenuFunList = await _roleMenuFunRepository.GetAllAsunc(x => x.role_id == request.dto.RoleId && x.del_flag == 0);
                foreach (var item in roleMenuFunList)
                {
                   
                    await _roleMenuFunRepository.DeleteAsync(item.id);
                }
                //删除角色菜单
                var roleMenulist = await _roleMenuRepository.GetAllAsunc(x => x.role_id == request.dto.RoleId && x.del_flag == 0);
                foreach (var item in roleMenulist)
                {
                  
                    await _roleMenuRepository.DeleteAsync(item.id);
                }
                //添加新的菜单功能
                foreach (var menuId in request.dto.MenuId)
                {

                    if (menuId.Contains("-"))
                    {
                        var menuIdArray = menuId.Split("-");
                        var roleMenuFunc = new sys_role_menu_fun
                        {
                            role_id = request.dto.RoleId,
                            menu_id = Convert.ToInt32(menuIdArray[0]),
                            fun_id = Convert.ToInt32(menuIdArray[1]),

                            create_by = request.UpdateName
                        };
                        await _roleMenuFunRepository.AddAsync(roleMenuFunc);
                    }
                    else
                    {
                        var roleMenu = new sys_role_menu
                        {
                            role_id = request.dto.RoleId,
                            menu_id = Convert.ToInt32(menuId),
                            create_by = request.UpdateName
                        };
                        await _roleMenuRepository.AddAsync(roleMenu);
                    }
                }
                return new ApiResult<int>() { Code = ResultCode.Ok, Message = "更新成功", Data = 1 };
            }
            catch (Exception ex)
            {

                throw;
            }

        }
    }
}
